// ListFileEngine.cpp: implementation of the CListFileEngine class.
//
//////////////////////////////////////////////////////////////////////

#include "ListFileEngine.h"


// ================= Constants =======================

// Number, name and date modified
_LIT(KStringDate,"\t%S\t\t");

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CListFileEngine::CListFileEngine()
{
	iDirList = NULL;
	iCurDir  = NULL;
	iFileExt = NULL;
}

CListFileEngine::~CListFileEngine()
{
	if (iDirList)
	{
		delete iDirList;
		iDirList = NULL;
	}

	if (iCurDir)
	{
		delete iCurDir;
		iCurDir = NULL;
	}

	if (iFileExt)
	{
		delete iFileExt;
		iFileExt = NULL;
	}
}

/*
TInt CListFileEngine::GetFileList()
{
	return 0;
}*/


// ---------------------------------------------------------
// CFileListEngine::StartFileList()
// This Method reads the appropriate directory list.
// ---------------------------------------------------------
//
TInt CListFileEngine::StartFileList()
{
    if (iDirList)
	{        
        delete iDirList;
        iDirList = 0;
	}    
	
    // Connect to fileserver
    User::LeaveIfError(iFsSession.Connect()); 
	  
	return iFsSession.GetDir(iCurDir->Des(), KEntryAttNormal, ESortByName, iDirList);
}

// ---------------------------------------------------------
// CFileListEngine::GetFileListItems(CDesCArray* aItems)
// This Method constructs the listbox items with directory 
// information
// ---------------------------------------------------------
//
void CListFileEngine::GetFileListItems(CDesCArray* aItems)
{
    if (!iDirList)
        return;
	
    for (TInt i=0;i<iDirList->Count();i++)
	{
        TFileName filename = NULL;
		
		// Fix the date and time string of last modification
		//TBuf<120> dateString; 
		//_LIT(KDateString,"%D%M%Y%/0%1%/1%2%/2%3%/3 %-B%:0%J%:1%T%:2%S%:3%+B");
		//(*iDirList)[i].iModified.FormatL(dateString,KDateString);
		

		// Show date modified
		//_LIT(KStringDate,"%d\t%S\t%S"); 
		//_LIT(KStringDate2,"\t%S\t\t");
		filename.Format(KStringDate, &(*iDirList)[i].iName);
		
		RDebug::Print(filename);
        aItems->AppendL(filename);
		//aItems->AppendL(_L("\tItem3\t\t"));
		//AddListBoxItemL(iListBox, _L("\tItem3\t\t"));
	}
	
}

// ---------------------------------------------------------
// CFileListEngine::SetDirectory(TInt aDirectory)
// This Method sets which directory to list.
// ---------------------------------------------------------
//
void CListFileEngine::SetDirectory(const TDesC& aDirectory, const TDesC& aExtension)
{
	if (iCurDir)
	{
		delete iCurDir;
	}

	iCurDir = HBufC::NewL(aDirectory.Length()+1);
	TPtr p = iCurDir->Des();

	p.Copy(aDirectory);
	p.ZeroTerminate();

	//RDebug::Print(iCurDir->TDesC16());
	
	// Store extension
	if (iFileExt)
	{
		delete iFileExt;
	}

	iFileExt = HBufC::NewL(aExtension.Length()+1);
	TPtr p2 = iFileExt->Des();

	p2.Copy(aExtension);
	p2.ZeroTerminate();
}


// ---------------------------------------------------------
// CFileListEngine::EndFileList()
// This Method ends the FileList session
// ---------------------------------------------------------
//
void CListFileEngine::EndFileList()
{
    // Close the file server session
    iFsSession.Close();
};

TInt CListFileEngine::GetCurDir(TDes& aDir)
{
	aDir.Copy(iCurDir->Ptr());
	return 0;
}

TInt CListFileEngine::GetFile(TFileName& aPath, TInt aIndex)
{
	aPath.Copy(iCurDir->Ptr());
	aPath.Append((*iDirList)[aIndex].iName);
	return 0;
}